package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Logger;

public class Database {

	public static void initialize() throws ClassNotFoundException {
		Class.forName("org.sqlite.JDBC");
	}

	private Connection connection;
	private DataDAO dataDAO;
	private UsersDAO usersDAO;

	public Database() {
		try{
			usersDAO = new UsersDAO(this);
			dataDAO = new DataDAO(this);
			connection = null;	
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		}catch(SQLException e){
			e.printStackTrace();
		}
		
	}

	public DataDAO getDataDAO() {
		return dataDAO;
	}

	public UsersDAO getUsersDAO() {
		return usersDAO;
	}

	public Connection getConnection() {
		return connection;
	}

	public void startTransaction(){
		try{
			connection = DriverManager
					.getConnection("jdbc:sqlite:./database/indexer-server.sqlite");
			connection.setAutoCommit(false);
		}catch(SQLException e){
			e.printStackTrace();
		}
		
	}

	public void endTransaction(boolean commit) {
		try{
			if (commit) {
				connection.commit();
			} else {
				connection.rollback();
			}
			connection.close();
		}catch(SQLException e){
			e.printStackTrace();
		}
		
	}
}
